#include "UdpSocket.h"


UdpSocket::UdpSocket(Listener *listener, std::string& ip,  uint16_t port, SocketFlags &flags):UdpSocketHandle(PortManager::BindUdp(ip, port, flags)), listener(listener), fixedPort(true) {}

// UdpSocket::UdpSocket(Listener *listener, std::string& ip, 
//     uint16_t minPort, uint16_t maxPort, SocketFlags &flags, uint64_t& portRangeHash): UdpSocketHandle() {
    
// }

UdpSocket::~UdpSocket() {
    if (!this->fixedPort) {
        PortManager::Unbind(this->portRangeHash, this->localPort);
    }
}

void UdpSocket::UserOnUdpDatagramReceived(const uint8_t* data, size_t len,
                                           const struct sockaddr* addr) {

    if (this->listener == nullptr) {
        printf("UdpSocket::UserOnUdpDatagramReceived() | no listener set, dropping packet\n");
        return;
    }
    if (this->listener) {
        this->listener->OnUdpSocketPacketReceived(this, data, len, addr);  
    }                                 
}